package com.zqw.action;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.info.UserInfo;
import com.zqw.pojo.AdminPojo;
import com.zqw.pojo.AuthPojo;
import com.zqw.pojo.RolePojo;
import com.zqw.pojo.UserPojo;
import com.zqw.service.UserService;
import com.zqw.util.MD5Util;
import com.zqw.util.StringUtil;
import com.zqw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    //转发到welcome
    @RequestMapping("welcome")
    public String welcome() {
        return "welcome";
    }

    //跳转到会员列表界面
    @RequestMapping("customerList")
    public String customerList(UserInfo userInfo, HttpServletRequest request) {
        PageHelper.startPage(userInfo.getPageNum(), userInfo.getPageSize());
        List<UserPojo> userPojos = userService.queryCustomer(userInfo);
        PageInfo<UserPojo> pageInfo = new PageInfo<>(userPojos);
        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("cname", userInfo.getCname());
        return "member-list";
    }

    //删除会员
    @RequestMapping("delCustomer")
    @ResponseBody
    public ResponseMsg delCustomer(String cid) {
        ResponseMsg rm = new ResponseMsg();
        String[] cids;
        if (cid.contains(",")) {
            cids = cid.split(",");
        } else {
            cids = new String[]{cid};
        }
        boolean b = userService.delCustomer(cids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //会员状态更改
    @RequestMapping("updCustomerStatus")
    @ResponseBody
    public ResponseMsg updCustomerStatus(UserInfo userInfo) {
        ResponseMsg rm = new ResponseMsg();
        boolean b = userService.updCustomerStatus(userInfo);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //跳转到修改会员界面
    @RequestMapping("customerEdit")
    public String customerEdit(UserInfo userInfo,HttpServletRequest request){
        List<UserPojo> userPojos = userService.queryCustomer(userInfo);
        if(userPojos != null&& userPojos.size()>0){
            request.setAttribute("userPojo",userPojos.get(0));
            return "member-edit";
        }else{
            return "error";
        }
    }

    //修改会员
    @RequestMapping("updCustomer")
    @ResponseBody
    public ResponseMsg updCustomer(MultipartFile myfile,UserInfo userInfo,HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String customerImage = request.getServletContext().getRealPath("customerImage");
            File file = new File(customerImage);
            if(!file.exists()){
                file.mkdir();
            }
            customerImage = customerImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(customerImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                userInfo.setCimage(File.separator+request.getContextPath().substring(1)+File.separator+customerImage.substring(customerImage.lastIndexOf(File.separator,customerImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = userService.updCustomer(userInfo);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到添加会员界面
    @RequestMapping("customerAdd")
    public String customerAdd(){
        return "member-add";
    }

    //添加会员
    @RequestMapping("addCustomer")
    @ResponseBody
    public ResponseMsg addCustomer(UserInfo userInfo){
        ResponseMsg rm = new ResponseMsg();
        userInfo.setCpass(MD5Util.getMd5(userInfo.getCpass()));
        boolean regist = userService.regist(userInfo);
        if (regist) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("添加失败");
        }
        return rm;
    }

    //跳转到修改密码界面
    @RequestMapping("customerPassEdit")
    public String customerPassEdit(String cid,HttpServletRequest request){
        request.setAttribute("cid",cid);
        return "member-password";
    }

    //修改密码
    @RequestMapping("updCustomerPass")
    @ResponseBody
    public ResponseMsg  updCustomerPass(UserInfo userInfo){
        ResponseMsg rm = new ResponseMsg();
        userInfo.setCpass(MD5Util.getMd5(userInfo.getCpass()));
        userInfo.setOldpass(MD5Util.getMd5(userInfo.getOldpass()));
        boolean b = userService.updPass(userInfo);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

}
